package com.glory.gloryquickui.viewmodel

import android.app.Activity
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import com.glory.gloryquickui.base.App
import com.glory.gloryquickui.base.BaseViewModel
import com.glory.gloryquickui.utils.DialogUtils
import com.glory.gloryquickui.utils.InitUtils
import com.glory.gloryquickui.utils.SharePreferencesUtils
import com.glory.gloryquickui.widget.dialog.FreeUI_GeneralFragmentDialog
import com.glory.gloryquickui.widget.dialog.callback.FreeUI_TextCallback
import com.glory.hiwork.mvvm.bean.CallObserveBean
import com.glory.hiwork.mvvm.bean.ToastMessageBean

/**
 * FileName: SettingViewModel
 * Author: XJH
 * Date: 2021/6/2 17:24
 * Description:
 * History:
 */
class SettingViewModel : BaseViewModel() {

    private var dialog: FreeUI_GeneralFragmentDialog? = null

    fun initVersion(context: Activity){
        val manager: PackageManager = context.packageManager
        var info: PackageInfo? = null
        try {
            info = manager.getPackageInfo(context.packageName, 0)
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        val version = info!!.versionName
        defUI.callObserve.value = CallObserveBean(0,version)
    }

    /**
     * type 1代表IP  2 代表FILE_URL
     */
    fun showScanDialog(context: AppCompatActivity, type : Int){
        dialog = DialogUtils.getScanDialog(context, if(type == 1)"请输入服务器地址" else "请输入文件地址",if (type == 1) InitUtils.BaseUrl else InitUtils.FileUrl,
            object : FreeUI_TextCallback{
            override fun onSuccess(var1: String) {
                if (type == 1) InitUtils.Companion.BaseUrl = var1 else InitUtils.Companion.FileUrl = var1

                defUI.toastEvent.value = ToastMessageBean("保存成功", true)
                defUI.callObserve.value = CallObserveBean(type,var1)
                SharePreferencesUtils.setSharePre("GLORY_QUICK_UI_FILE", if(type == 1)"BASE_URL" else "FILE_URL", var1, InitUtils.app)
                dialog?.dismiss()
            }

        })
        dialog?.show(context.supportFragmentManager, "WEB_URL_DIALOG")
    }
}